﻿using PMS.Core.Configuration.CommonEnumConfigs;
using PMS.Data.Entities.Stores;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace PMS.Data.Entities.SystemModules
{
    /// <summary>
    /// SystemModule associate with CustomerRole for Permissions
    /// </summary>
    public partial class SystemModule : BaseEntity, IStoreMappingSupported
    {
        #region Configs
        public const int ROOTMODULEDEPTH = 1;
        public const int ROOTMODULEID = 0;
        private const string _urlPattern = @"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$";
        #endregion

        #region Fields
        private string _displayname;
        private string _moduleurl;
        #endregion

        #region Ctor
        /// <summary>
        /// Ctor Setup Default Values
        /// </summary>
        public SystemModule()
        {
            _displayname = string.Empty;
            _moduleurl = string.Empty;
            ModuleDepth = ROOTMODULEDEPTH;
            ParentModuleId = ROOTMODULEID;
            OpenMode = LinkOpenMode.None;
            LimitedToStores = false;
            IsActive = true;
            DisplayOrder = 1;
            CreatedOnTimeUtc = UpdatedOnTimeUtc = DateTime.UtcNow;
        }
        #endregion

        #region Utils
        /// <summary>
        /// Matches http url such as:
        /// "http://www.example.com/wpstyle/?p=364"
        /// "http://example.com/questions/12576252/convert-javascript-regex-to-c-sharp-regex-for-email-validation"
        /// "www.example.com/doc"
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        protected bool IsMatchUrl(string url)
        {
            Regex regex = new Regex(_urlPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return regex.IsMatch(url);
        }
        #endregion

        /// <summary>
        /// Gets and sets the Name of the ModuleButton
        /// </summary>
        public string Name { get; set; }
        
        /// <summary>
        /// Gets and sets Name output on HTML 
        /// </summary>
        public string DisplayName 
        { 
            get 
            {
                if (string.IsNullOrEmpty(_displayname))
                    return Name;
                return _displayname;
            }
            set => _displayname = value;
        }

        /// <summary>
        /// Gets and sets Descript info for Module 
        /// </summary>
        public string ModuleDescription { get; set; }

        /// <summary>
        /// 系统模块按树形结构分层层级深度，父子结构树层级顺序加一
        /// </summary>
        public int ModuleDepth { get; set; }
       
        /// <summary>
        /// 系统模块按树级结构加载形成，根节点ParentModuleId为0，深度为1
        /// </summary>
        public int  ParentModuleId { get; set; }

        /// <summary>
        /// 系统模块在HTML页面中使用图标标签的CSS Class
        /// </summary>
        public string HtmlIconCSSClass { get; set; }

        /// <summary>
        /// Friendly Url for ModulePage
        /// </summary>
        public string RouteUrl 
        {
            get => _moduleurl;
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    //MakeSure Url start with '/' looked like "/controller/action"
                    //if Url is LocalPath
                    if (!value.StartsWith('/') && !IsMatchUrl(value))
                        _moduleurl = "/" + value;
                }
                _moduleurl = value;
            } 
        }

        /// <summary>
        /// Gets or sets a value indicating whether the entity is limited/restricted to certain stores
        /// </summary>
        public bool LimitedToStores { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        public int DisplayOrder { get; set; }

        /// <summary>
        /// 系统模块打开方式 导航栏/新窗口/页面跳转
        /// </summary>
        public LinkOpenMode OpenMode { get; set; }

        /// <summary>
        /// 当为系统模块时可分配指定模块按钮在页面中显示，存放模块按钮Id数组，通过逗号分隔
        /// </summary>
        public string AssignedModuleButtonIds { get; set; }
      
        public IList<int> ModuleButtonIds
        {
            get => ConvertFromString(AssignedModuleButtonIds);
            set => AssignedModuleButtonIds = ConvertFromArray(value);
        }
        //Setted on BaseEntity
        //public bool IsActive { get; set; }
        //public DateTime CreatedOnTimeUtc { get; set; }
        //public DateTime UpdatedOnTimeUtc { get; set; }
    }
}
